home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / SWAP.CPP < prev    next >
Text File  |  1997-05-06  |  1KB  |  44 lines

  1.  #include <vector>
  2.  #include <algorithm>
  3.  
  4.  using namespace std;
  5.  
  6.  int main ()
  7.  {
  8.    int d1[] = {6, 7, 8, 9, 10, 1, 2, 3, 4, 5};
  9.    //
  10.    // Set up a vector.
  11.    //
  12.    vector<int> v(d1+0, d1+10);
  13.    //
  14.    // Output original vector.
  15.    //
  16.    cout << "For the vector: ";
  17.    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
  18.    //
  19.    // Swap the first five elements with the last five elements.
  20.    //
  21.    swap_ranges(v.begin(), v.begin()+5, v.begin()+5);
  22.    //
  23.    // Output result.
  24.    //
  25.    cout << endl << endl
  26.         << "Swaping the first five elements with the last five gives: "
  27.         << endl << "     ";
  28.    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
  29.    //
  30.    // Now an example of iter_swap -- swap first and last elements.
  31.    //
  32.    iter_swap(v.begin(), v.end()-1);
  33.    //
  34.    // Output result.
  35.    //
  36.    cout << endl << endl
  37.         << "Swaping the first and last elements gives: "
  38.         << endl << "     ";
  39.    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
  40.    cout << endl;
  41.  
  42.    return 0;
  43.  }
  44.